package com.project.springboot.controller;

import com.project.springboot.annotation.Log;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * description: 测试自定义日志注解
 * @author 朱正磊
 * @date 2025-05-12 15:36:13
 */
@RestController
@RequestMapping("/log")
public class LogController {

    /**
     * description: 获取用户信息
     * @author 朱正磊
     * @date 2025-05-12 15:49:35
     * @param id 唯一值
     * @param name 姓名
     * @return 返回用户信息
     */
    @Log("获取用户信息")
    @GetMapping("/getUserInfo/{id}")
    public Map<String, Object> getUserInfo(@PathVariable Long id, @RequestParam(required = false) String name) {
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("id", id);
        userInfo.put("name", name == null ? "default" : name);
        userInfo.put("age", 35);
        return userInfo;
    }

    /**
     * description: 新增用户信息
     * @author 朱正磊
     * @date 2025-05-12 15:49:44
     * @param user 用户信息
     * @return 返回用户信息
     */
    @Log(value = "新增用户信息", recordParams = true, recordResult = true)
    @PostMapping("/createUser")
    public Map<String, Object> createUser(@RequestBody Map<String, Object> user) {
        user.put("id", System.currentTimeMillis());
        return user;
    }

    /**
     * description: 测试服务异常
     * @author 朱正磊
     * @date 2025-05-12 15:49:53
     */
    @Log(value = "测试服务异常", recordResult = false)
    @GetMapping("/throwException")
    public void throwException() {
        throw new RuntimeException("服务异常");
    }

}
